package com.yuchting.dinneronline.client.common;

import com.google.gwt.json.client.JSONObject;

public class Dinner {
	
	/**
	 * get the rank number to get the rank
	 * @author yuch
	 *
	 */
	public static class RankNumber{
		
		/**
		 * integer part
		 */
		public int integerDigit;
		
		/**
		 * decimal digit part
		 */
		public int decimalDigit;
		
		public RankNumber(byte _rank){
			integerDigit = _rank >>> 4;
			decimalDigit = _rank & 0x0f;
		}
		
		/**
		 * get the rank number as a float
		 * @return float type of rank
		 */
		public float asFloat(){
			float t_ret = integerDigit;
			t_ret += (float)decimalDigit / 10;
			return t_ret;
		}
		
		public String toString(){
			return Float.toString(asFloat());
		}
	}
	
	private String m_name = "";
		
	private double m_price = 9.5;
	
	private int	m_orderNum = 0;
	
	// se xiang wei 
	private byte[]	m_rank		= {0,0,0};
	
	private long[] m_totalRank	= {0,0,0};
	
	private long	m_commentNum= 0;
	
	public Dinner(){}
		
	public String getName(){return m_name;}
	public void setName(String _name){m_name = _name;}
	
	public double getPrice(){return m_price;}
	public void setPrice(double _price){m_price = _price;}
	
	public int	getOrderNum(){return m_orderNum;}
	public void setOrderNum(int _num){m_orderNum = _num;}
	
	public long[] getTotalRank(){return m_totalRank;}
	public long getCommnetNum(){return m_commentNum;}
	
	public RankNumber getRand(int _index)throws IllegalArgumentException{
		if(_index < 0 || _index > m_rank.length){
			throw new IllegalArgumentException("_index out of arr size:" + m_rank.length);
		}
		
		return new RankNumber(m_rank[_index]); 
	}
		
	public JSONObject toJSON(){
		
		YuchJSONObject t_result = new YuchJSONObject();
		try{
			t_result.put("name",m_name);
			t_result.put("price",m_price);
			t_result.put("order_num",m_orderNum);
			t_result.put("commnet",m_rank);
			
			t_result.put("rank",m_rank);
			t_result.put("total_rank",m_rank);			
			
		}catch(Exception e){
			System.err.println("Dinner toJSON Error:");
			e.printStackTrace(System.err);
		}
		
		return t_result.m_json;
	}
		
	public void fromJSON(YuchJSONObject _json){
		
		try{
			m_name 		= _json.getString("name");
			m_price 	= _json.getDouble("price");
			m_orderNum	= _json.getInt("order_num");
			m_commentNum= _json.getLong("commnet");
			
			m_rank		= _json.getByteArray("rank");
			m_totalRank	= _json.getLongArray("total_rank");
			
			
		}catch(Exception e){
			System.err.println("Dinner fromJSON Error:");
			e.printStackTrace(System.err);
		}
	}
	
	
}
